package com.ssm.demo.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ssm.demo.model.vo.UserVo;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		UserVo user = (UserVo) request.getSession().getAttribute("user");
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		
		String url = request.getRequestURI();
		System.out.println(url);
		
		if (user != null && user.getUserId() != null) {
			return true;
		} else {
			response.sendRedirect(basePath);
			return false;
		}
	}

}
